package com.googlecode.gwtbb.client.shared.remote.forum;

import com.googlecode.gwtbb.client.shared.GwtbbSmess;
import com.googlecode.gwtbb.client.shared.entity.forum.Forum;
import com.googlecode.gwtbb.client.shared.entity.forum.Post;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.googlecode.gwtbb.client.shared.validation.GwtbbValidator;
import com.googlecode.gwtbb.client.shared.validation.Validateable;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Apr 30, 2008
 */
public class CreatePostBean implements Validateable, Serializable {
	protected transient Forum forum;
	protected Long forumId;
	protected Long threadId;
	protected String subject;
	protected String body;

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public Forum getForum() {
		return forum;
	}

	public Long getForumId() {
		return forumId;
	}

	public void setForumId(Long forumId) {
		this.forumId = forumId;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public Long getThreadId() {
		return threadId;
	}

	public void setThreadId(Long threadId) {
		this.threadId = threadId;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
		setForumId(forum != null ? forum.getId() : null);
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		subject = GwtbbValidator.max(failures, FieldId.create(Post.SUBJECT, index), subject, Post.SUBJECT_MAX);
		body = GwtbbValidator.max(failures, FieldId.create(Post.BODY, index), body, Post.BODY_MAX);
		if (StringUtils.isEmpty(subject) && StringUtils.isEmpty(body))
			failures.put(null, new Failure(GwtbbSmess.instance().postSubjectOrBodyRequired()));
		return failures;
	}
}